#include "ExtractionManagerWindow.h"

ExtractionManagerWindow::ExtractionManagerWindow(QDialog *parent) : QDialog(parent){
	setupUi(this);
	this->connectSlots();
	isPlaying = false;
	timer = new QTimer();
	connect(timer, SIGNAL(timeout()), this, SLOT(updateSlider()));
	timer->start(200);
}

ExtractionManagerWindow::~ExtractionManagerWindow(){
}

void ExtractionManagerWindow::connectSlots(){
	connect(this->listWidget, SIGNAL(currentRowChanged(int)), this, SLOT(changeExtraction(int)));
	connect(this->buttonLire, SIGNAL(clicked()), this, SLOT(playOrPause()));
	connect(this, SIGNAL(accepted()), this, SLOT(reduceWindow()));
	connect(this->spanSlider, SIGNAL(lowerValueChanged(int)), spinBoxDebut, SLOT(setValue(int)));
	connect(this->spanSlider, SIGNAL(upperValueChanged(int)), spinBoxFin, SLOT(setValue(int)));
	connect(spinBoxDebut, SIGNAL(valueChanged(int)), this->spanSlider, SLOT(setLowerValue(int)));
	connect(spinBoxFin, SIGNAL(valueChanged(int)), this->spanSlider, SLOT(setUpperValue(int)));
	connect(this->spanSlider, SIGNAL(lowerValueChanged(int)), this, SLOT(updateExtraction()));
	connect(this->spanSlider, SIGNAL(upperValueChanged(int)), this, SLOT(updateExtraction()));
	connect(spinBoxDebut, SIGNAL(valueChanged(int)), this, SLOT(updateExtraction()));
	connect(spinBoxFin, SIGNAL(valueChanged(int)), this, SLOT(updateExtraction()));
	connect(buttonSave, SIGNAL(clicked()), this, SLOT(saveExtractions()));

	connect(this->playSlider, SIGNAL(sliderMoved(int)), this, SLOT(seek(int)));
}

int ExtractionManagerWindow::getVideoWidgetId(){
	return (int)this->videoWidget->winId();
}

int ExtractionManagerWindow::getNExtractions(){
	return this->listWidget->count();
}

void ExtractionManagerWindow::addExtraction(int position){
	DataManagerSingleton::getInstance()->addExtraction(position);
	int nExtractions = getNExtractions();
	Extraction extraction = DataManagerSingleton::getInstance()->getExtraction(nExtractions);
	QString extractionName;
	int fps = DataManagerSingleton::getInstance()->getFps();
	extractionName.sprintf("%06d => %06d", extraction.start/fps, extraction.end/fps); //TODO il faudrait que je mete des secondes, ce serait mieux
	this->listWidget->addItem(extractionName);
}

void ExtractionManagerWindow::play(){
	DataManagerSingleton::getInstance()->play();
	isPlaying = true;
	this->buttonLire->setText("Pause");
}

void ExtractionManagerWindow::pause(){
	DataManagerSingleton::getInstance()->pause();
	isPlaying = false;
	this->buttonLire->setText("Lire");
}

void ExtractionManagerWindow::playOrPause(){
	if(isPlaying){
		this->pause();
	}else{
		this->play();
	}
}

void ExtractionManagerWindow::reduceWindow(){
	this->pause();
	DataManagerSingleton::getInstance()->stopConsumer();
}

void ExtractionManagerWindow::changeExtraction(int rowIndex){
	DataManagerSingleton::getInstance()->changeExtraction(rowIndex);
	//this->pause();
	Extraction extraction = DataManagerSingleton::getInstance()->getExtraction(rowIndex);
	spinBoxDebut->setValue(extraction.start);
	spinBoxFin->setValue(extraction.end);
	//int lengthVideoInFrame = DataManagerSingleton::getInstance()->getLengthVideo();
	//int lengthSpanSlider = this->spanSlider->maximum();
	int lowerValue = extraction.start;
	int upperValue = extraction.end;
	if(lowerValue < this->spanSlider->upperValue()){
		this->spanSlider->setLowerValue(lowerValue);
		this->spanSlider->setUpperValue(upperValue);
	}else{
		this->spanSlider->setUpperValue(upperValue);
		this->spanSlider->setLowerValue(lowerValue);
	}
}

void ExtractionManagerWindow::setSlidersMaximum(int maximum){
	this->spanSlider->setMaximum(maximum);
	this->playSlider->setMaximum(maximum);
}

void ExtractionManagerWindow::updateExtraction(){
	int currentRowIndex = listWidget->currentRow();
	Extraction newExtraction;
	newExtraction.start = spinBoxDebut->value();
	newExtraction.end = spinBoxFin->value();
	DataManagerSingleton::getInstance()->setExtraction(currentRowIndex, newExtraction);
	if(newExtraction.start > this->playSlider->value()){
		this->playSlider->setValue(newExtraction.start);
		DataManagerSingleton::getInstance()->seek(newExtraction.start);
	}
}

void ExtractionManagerWindow::seek(int position){
	if(position < spinBoxDebut->value()){
		DataManagerSingleton::getInstance()->seek(spinBoxDebut->value());
	}else if(position <= spinBoxFin->value()){
		DataManagerSingleton::getInstance()->seek(position);
	}else{
		DataManagerSingleton::getInstance()->seek(spinBoxFin->value());
	}
}

void ExtractionManagerWindow::updateSlider(){
	if(!this->playSlider->isSliderDown()){
		int currentPosition = DataManagerSingleton::getInstance()->getPosition();
		if(currentPosition < spinBoxFin->value()){
			this->playSlider->setValue(currentPosition);
		}else{
			pause();
		}
	}
}

void ExtractionManagerWindow::saveExtractions(){
	QString fileName = QFileDialog::getSaveFileName(this, "Enregistrer les extractions", QString(), "DVD (*.dvd)");
	if(!fileName.isNull()){
		if(!fileName.endsWith(".dvd")){
			fileName += ".dvd";
		}
		cout << "DEBUG : fileName : " << fileName.toStdString() << endl;
		DataManagerSingleton::getInstance()->saveExtractions(fileName);
		QMessageBox::information(this, "Fichier", "Fichier enregistre avec succes !");
	}
}

